#include <stdlib.h>

#include "fsl_debug_console.h"
#include "tarm_uart.h"

//middleware
#include "tarm_ctimer.h"
//hardware
#include "tarm_time.h"

#include "application/tarm_run.h"

/************* �ص��ӿڣ�middleware���½ӿڣ���application **************/
void Time0CallBack(void)
{
	handle_interrupt();
	TimeBase_ClearFlag();
}

void Time1CallBack(void)
{
#ifdef _DEBUG_TARM_SPEED_DELAY_TIME_
//	bool t_isCoounterSpeedDebugFlag = false;
	static bool s_isHighSize = true;
	s_isHighSize = !s_isHighSize;

	if(s_isHighSize == false)	//only the second size waste much time
	if(ReportStruct.SpeedDebugStruct.ReportFlag == 2)
	{
		SpeedDebugTimer_start();
		ReportStruct.SpeedDebugStruct.ReportFlag = 3;
	}
	else if(ReportStruct.SpeedDebugStruct.ReportFlag == 3)
	{
		SpeedDebugTimer_stop();
		ReportStruct.SpeedDebugStruct.MinTimeForReally = SpeedDebugTimer_read();
		SpeedDebugTimer_clear();
		SpeedDebugTimer_start();
		ReportStruct.SpeedDebugStruct.ReportFlag = 0;

	}
	else if(ReportStruct.SpeedDebugStruct.ReportFlag == 0)
	{
		SpeedDebugTimer_stop();
		int t_speedDebugReallyRecord = SpeedDebugTimer_read();
		if(t_speedDebugReallyRecord!=0)
			if(t_speedDebugReallyRecord < ReportStruct.SpeedDebugStruct.MinTimeForReally)
			{
				ReportStruct.SpeedDebugStruct.MinTimeForReally = t_speedDebugReallyRecord;
				ReportStruct.SpeedDebugStruct.MinTimeForTheory = TimePV_getNowTimer()*2;
//				t_isCoounterSpeedDebugFlag = true;

			}
		SpeedDebugTimer_clear();
		SpeedDebugTimer_start();
	}

#endif
	handle_pvinterrupt(0);
	TimePV_ClearFlag();
#ifdef _DEBUG_TARM_SPEED_DELAY_TIME_
	//t_speedDebugTheoryRecord = SpeedDebugTimer_read();
//	if(t_isCoounterSpeedDebugFlag == true){
//
//		t_isCoounterSpeedDebugFlag = false;
//	}
#endif
}

//************* TimeBase **************
void TimeBase_Config(void)
{
	CTIMER0_InitXUs(100);
}

void TimeBase_InitUnstart(void)
{
	TimeBase_Config();
	TimeBase_Disable();
}

void TimeBase_Reset(void)
{
	CTIMER0_Reset();
}

void TimeBase_Enable(void)
{
	CTIMER_StartTimer(CTIMER0);
}

void TimeBase_Disable(void)
{
	CTIMER_StopTimer(CTIMER0);
}

void TimeBase_ClearFlag(void)
{
//	CTIMER_ClearStatusFlags(CTIMER0, CTIMER_IR_MR0INT_MASK);
}


//************* TimePV **************
void TimePV_Config(int t_timeUs)
{
	CTIMER1_InitXUs(t_timeUs);
}

void TimePV_InitUnstart(int t_timeUs)
{
	CTIMER1_InitXUs(t_timeUs);
	TimePV_Disable();
}

void TimePV_Reset(void)
{
	CTIMER1_Reset();
}

void TimePV_Start(int t_timeUs)
{
#ifdef _DEBUG_TARM_SPEED_DELAY_TIME_
	if(ReportStruct.SpeedDebugStruct.ReportFlag!=1 && strlen(ReportStruct.SpeedDebugStruct.Buffer)<1000)
		if(t_timeUs<500)
		{
			strcat(ReportStruct.SpeedDebugStruct.Buffer, "|");
		}
		else
			strcat(ReportStruct.SpeedDebugStruct.Buffer, "/");
#endif
	CTIMER1_ChangeMatchValue(t_timeUs);
	TimePV_Enable();
}

void TimePV_Enable(void)
{
	CTIMER_StartTimer(CTIMER1);
}

void TimePV_Disable(void)
{
	CTIMER_StopTimer(CTIMER1);
}

void TimePV_ClearFlag(void)
{
//	CTIMER_ClearStatusFlags(CTIMER1, CTIMER_IR_MR0INT_MASK);
}

uint32_t TimePV_getNowTimer(void)
{
	return CTIMER1->MR[0];
}

//************* debug timer **************
void SpeedDebugTimer_init()
{
	CTIMER2_Init();
}

void SpeedDebugTimer_start()
{
	CTIMER_StartTimer(CTIMER2);
}

void SpeedDebugTimer_stop()
{
	CTIMER_StopTimer(CTIMER2);
}

void SpeedDebugTimer_clear()
{
	CTIMER2->TC = 0;
}

uint32_t SpeedDebugTimer_read()
{
	return CTIMER_GetTimerCountValue(CTIMER2);
}
